package com.talent.jiwen_teacher.my;

import android.content.DialogInterface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.talent.jiwen_teacher.R;
import com.talent.jiwen_teacher.base.BaseActivity;
import com.talent.jiwen_teacher.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen_teacher.http.common.ApiFactory;
import com.talent.jiwen_teacher.http.common.HttpUtil;
import com.talent.jiwen_teacher.http.common.ProgressSubscriber;
import com.talent.jiwen_teacher.http.result.DiscreditListResult;
import com.talent.jiwen_teacher.util.DateUtils;
import com.talent.jiwen_teacher.util.SPUtil;
import com.talent.jiwen_teacher.util.Validators;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;


/**
 * Created by qyqx on 2018/8/24.
 * 综合信用
 */

public class CreditActivity extends BaseActivity {

    @BindView(R.id.img_state)
    ImageView img_state;

//    @BindView(R.id.rl_to_take)
//    RelativeLayout rl_to_take;

    @BindView(R.id.credit_recyclerView)
    RecyclerView credit_recyclerView;

    @BindView(R.id.tv_title)
    TextView tv_title;

    @BindView(R.id.noDataIv)
    ImageView noDataIv;

    @BindView(R.id.credit_text_right)
    TextView credit_text_right;

    @BindView(R.id.credit_btn_left)
    ImageView credit_btn_left;

    @BindView(R.id.txt_count)
    TextView  txt_count;

    @BindView(R.id.txt_dong)
    TextView txt_dong;


//    private Handler handler = new Handler();

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_credit;
    }

    @Override
    public String titleName() {
        return "综合信用";
    }

    List <DiscreditListResult.Discredit> dataList=new ArrayList<>();

    CommonAdapter<DiscreditListResult.Discredit> adapter;

    @Override
    protected void initData() {

        mHeadLayout.setVisibility(View.GONE);
        credit_text_right.setText("规则");

        tv_title.setText("综合信用");

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(CreditActivity.this,
                LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        credit_recyclerView.setLayoutManager(linearLayoutManager);
        setAdapter();

//        LoadWhiteDialog.show(CreditActivity.this);
        getDiscreditList();
    }


    private void setAdapter(){
        adapter=new CommonAdapter<DiscreditListResult.Discredit>(CreditActivity.this,R.layout.item_credit_record,dataList) {
            @Override
            protected void convert(ViewHolder holder, final DiscreditListResult.Discredit discredit, int position) {
                holder.setText(R.id.txt_time, DateUtils.ms2Date(discredit.getCreateTime()));
                holder.setText(R.id.txt_reason, "上课5分钟内未到");
//                holder.getView(R.id.txt_su).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        OrderAppealActivity.startOrderAppealActivity(CreditActivity.this , OrderAppealActivity.APPEAL_CREADIT , discredit.getOrderId() , "");
//                    }
//                });
            }
        };
        credit_recyclerView.setAdapter(adapter);
    }

    @OnClick({R.id.credit_btn_left,R.id.credit_text_right})//R.id.rl_to_take,
    public void onViewClicked(View view) {
        switch (view.getId()) {
//            case R.id.rl_to_take:
//                break;
            case R.id.credit_btn_left:
                finish();
                break;
            case R.id.credit_text_right:
                new MaterialDialog.Builder(this)
                        .title("即问信用规则")
                        .content("“即问”采取信用分的方式对老师进行监督。信用分为评价老师个人在即问app中课程是否按照规定时间进行相关操作的参照。\n" +
                                "信用分最高上限为3分，最低下限为0分。老师每有一次失信行为就会扣除1分信用分。扣除的1分信用分对应处罚是冻结老师端用户100元余额（不足100元的全部冻结）。如信用分为0分，对应的冻结资金即为300元。\n" +
                                 "如失信行为超过3次的，则“即问”将冻结用户的账户，届时用户的账户将无法提现，app的功能也无法正常使用。具体解除限制的方案需联系“即问”的客服专员。\n" +
                                "如自前一次失信行为开始，老师连续10次课程不存在失信行为，则可以加1分信用分。信用分达到3分时，不再继续加分。\n"+"上述的失信行为是指老师进入订单课堂的时间超过规定时间5分钟的行为。")
                        .positiveText("确定")
                        .showListener(new DialogInterface.OnShowListener() {
                            @Override
                                public void onShow(DialogInterface dialog) {
                                }
                        })
                        .cancelListener(new DialogInterface.OnCancelListener() {
                            @Override
                            public void onCancel(DialogInterface dialog) {
                            }
                        })
                        .dismissListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialog) {
                            }
                        }).show();
                break;
        }
    }
    /**获取老师失信记录
     *
     */
    private void getDiscreditList(){
        Map<String , String> map = new HashMap<>();
        map.put("teacherId" , SPUtil.getUserId());

        Observable observable = ApiFactory.getEducationApiSingleton().getDiscreditList(SPUtil.getToken() , map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<DiscreditListResult>(CreditActivity.this) {
            @Override
            protected void _onNext(DiscreditListResult result) {
                dataList.clear();
                dataList.addAll(result.getDiscreditList());
                adapter.notifyDataSetChanged();

                if (Validators.isEmpty(dataList)){
                    noDataIv.setVisibility(View.VISIBLE);
                }else{
                    noDataIv.setVisibility(View.GONE);
                }

                if(result.getDiscreditCount()<=3){
                    txt_count.setText("信用分："+ (3 - result.getDiscreditCount()));
                }else{
                    txt_count.setText("信用分：0");
                }

                if(result.getDiscreditCount()==0){
                    txt_dong.setVisibility(View.GONE);
                }else{
                    txt_dong.setVisibility(View.VISIBLE);
                    if(result.getDiscreditCount()<=3){
                        txt_dong.setText("需冻结金额："+ (result.getDiscreditCount())*100);
                    }else{
                        txt_dong.setText("需冻结金额：300");
                    }

                }

                if (result.getDiscreditCount() < 1){
//                    rl_to_take.setVisibility(View.GONE);
                    img_state.setImageResource(R.mipmap.img_good);
                }else if (result.getDiscreditCount() >= 3){
//                    rl_to_take.setVisibility(View.VISIBLE);
                    img_state.setImageResource(R.mipmap.img_dong);
                }else{
//                    rl_to_take.setVisibility(View.GONE);
                    img_state.setImageResource(R.mipmap.img_up);
                }

//                handler.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        LoadWhiteDialog.dismiss(CreditActivity.this);
//                    }
//                } , 1000L);
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
//                handler.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        LoadWhiteDialog.dismiss(CreditActivity.this);
//                    }
//                } , 1000L);

            }
        } , ActivityLifeCycleEvent.DESTROY , lifecycleSubject , true);
    }



}
